package com.glory.dmegcpda.viewmodel

import androidx.databinding.ObservableArrayList
import com.glory.dmegcpda.R
import com.glory.dmegcpda.bean.BindBatteryBean
import com.glory.dmegcpda.bean.MLotListBean
import com.glory.dmegcpda.model.MaterialReceiveRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: MaterialReceiveViewModel
 * Author: XJH
 * Date: 2021/9/27 15:20
 * Description:
 * History:
 */
class BindBatteryViewModel : BaseViewModel() {

    val materialTitle = arrayListOf("物料号","批号","箱号/包号", "数量")
    val batteryRepository by lazy { MaterialReceiveRepository() }

    //物料列表
    var mBatteryItems = ObservableArrayList<BindBatteryBean>()
    var mBatteryBinding =
        ItemBinding.of<BindBatteryBean>(BR.mBatteryItem, R.layout.item_battery)

    fun submit(){
        val map = JsonObject()
        map.addProperty("ACTIONTYPE","RECEIVEININV")
        map.add("MLOTLIST", JsonParser().parse(Gson().toJson(mBatteryItems)).asJsonArray)
        launchOnlyResult({
            batteryRepository.bindBattery(map)
            defUI.toastEvent.value = ToastMessageBean("电池片绑定成功",true)
            defUI.startActivity.call()
        })
    }

    fun addData(box : String,lot : String,num : String){
        val entity  = BindBatteryBean()
        entity.MLOTID = box
        entity.TRANSMAINQTY = num
        entity.MATERIALNAME = lot.split("|")[0]
        entity.PARTNERLOTID = lot.split("|")[1]
        entity.TRANSWAREHOUSEID = "INV_M3"
        mBatteryItems.add(entity)
        defUI.callObserve.call()

    }


}